#include "wizard.h"
#include "ui_wizard.h"

Wizard::Wizard(QWidget *parent) :
    QWizard(parent),
    ui(new Ui::Wizard)
{
    ui->setupUi(this);
    teststyle_model_wizard=new QSqlQueryModel();
    teststyle_model_wizard->setQuery("select teststyle,ID from teststyle");
    ui->comboBox_wizard_teststyle->setModel(teststyle_model_wizard);
    ui->comboBox_wizard_teststyle->setModelColumn(0);

    subject_model_wizard=new QSqlQueryModel();
    subject_model_wizard->setQuery("select subject,ID from subject");
    ui->comboBox_wizard_subject->setModel(subject_model_wizard);
    ui->comboBox_wizard_subject->setModelColumn(0);
    //ui->listWidget->setModel();
    ui->tableWidget->setColumnCount(3);
    QStringList headers;
    headers << tr("科目") << tr("题型") << tr("题数");
    ui->tableWidget->setHorizontalHeaderLabels(headers);
    ui->tableWidget->setColumnWidth(0,50);
    ui->tableWidget->setColumnWidth(1,50);
    ui->tableWidget->setColumnWidth(2,50);
    ui->tableWidget->setSelectionBehavior ( QAbstractItemView::SelectRows); //设置选择行为，以行为单位
    ui->tableWidget->setSelectionMode ( QAbstractItemView::SingleSelection); //设置选择模式，选择单行
    ui->tableWidget->setEditTriggers ( QAbstractItemView::NoEditTriggers );  //禁止编辑




}

Wizard::~Wizard()
{
    delete ui;
}

void Wizard::on_ButtonFilter_clicked()
{
    QSqlQuery newquery;
    newquery.prepare(QString("select count(*) as amount from test where subjectID=%1 and teststyleID=%2")
                     .arg(m_wizard_subjectID)
                     .arg(m_wizard_teststyleID)
                     );
    //newquery.prepare(QString("select count(*) as amount from test"));
    if(!newquery.exec())
    {
    qDebug()<<"[wizard get amount failed]";
    }
    else
    {
    qDebug()<<"[wizard get amount ok]\n";
    }
    if (newquery.next())
    {
        qDebug()<<newquery.value(0).toInt();
    }
    //QString str="%1";
    //str.arg(newquery.value(0).toInt());
    qDebug()<<"m_wizard_subjectID"<<m_wizard_subjectID<<"m_wizard_teststyleID"<<m_wizard_teststyleID;
              //newquery.value(0).toInt();
    ui->lineEdit_Amount->setText(QString("%1").arg(newquery.value(0).toInt()));
}

void Wizard::on_comboBox_wizard_subject_currentIndexChanged ( int index )
{
    m_wizard_subjectID=subject_model_wizard->data(subject_model_wizard->index(index,1)).toInt();
    qDebug()<<m_wizard_subjectID;
}

void Wizard::on_comboBox_wizard_teststyle_currentIndexChanged ( int index )
{
    m_wizard_teststyleID=teststyle_model_wizard->data(teststyle_model_wizard->index(index,1)).toInt();
    qDebug()<<m_wizard_teststyleID;
}


void Wizard::on_ButtonAddToList_clicked()
{
  //  qDebug()<<"add to list";
  //  ItemList.append("test");
 //   new QListWidgetItem(ItemList,ui->listWidget);
     // new QListWidgetItem(tr("Hazel"), ui->listWidget);

    //ui->listWidget->addItem(ItemList);
    ui->tableWidget->setRowCount(ui->tableWidget->rowCount()+1);
    qDebug()<<ui->tableWidget->rowCount();
    ui->tableWidget->setItem(ui->tableWidget->rowCount()-1, 0,
                             new QTableWidgetItem(QString(ui->comboBox_wizard_subject->currentText())));
    ui->tableWidget->setItem(ui->tableWidget->rowCount()-1, 1,
                             new QTableWidgetItem(QString(ui->comboBox_wizard_teststyle->currentText())));
    ui->tableWidget->setItem(ui->tableWidget->rowCount()-1, 2,
                             new QTableWidgetItem(QString(ui->lineEdit_QuestionNumber->text())));
    //TreeItemRoot->setData(0,Qt::UserRole,rootquery.value(0).toString());//FIELD:ID
    ui->tableWidget->item(ui->tableWidget->rowCount()-1,0)->setData(Qt::UserRole,m_wizard_subjectID);
    ui->tableWidget->item(ui->tableWidget->rowCount()-1,1)->setData(Qt::UserRole,m_wizard_teststyleID);
}

void Wizard::on_ButtonRemoveRow_clicked()
{
    if(!ui->tableWidget->selectedItems().isEmpty())
    {
        ui->tableWidget->removeRow(ui->tableWidget->row(ui->tableWidget->selectedItems().first()));
    }
}

//QDialog::finished ( int result )
void Wizard::accept()
{
    if(ui->lineEdit_PaperName->text()=="")
    {
        QMessageBox::information(NULL, "testbase ", tr("哎呀，试卷名字还没起呢！"));
        return;
     }
    if(ui->tableWidget->rowCount()<=0)
    {
        QMessageBox::information(NULL, "testbase ", tr("哎呀，试卷题目还没选呢！"));
        return;
    }

    qDebug()<<"abc"<<ui->lineEdit_PaperName->text();
    QSqlQuery newquery;
    newquery.prepare(QString("insert into testpaper(title) values (:1)"));
    newquery.bindValue(":1",ui->lineEdit_PaperName->text());

    if(!newquery.exec())
    {
        qDebug()<<"[wizard get amount failed]";
    }
    else
    {
       qDebug()<<"[wizard get amount ok]\n";
    }
    m_wizard_PaperID=newquery.lastInsertId().toInt();
    qDebug()<<"m_wizard_PaperID"<<m_wizard_PaperID;

    //SELECT id,subjectid FROM test where ID>100 and ID<150 ORDER BY RANDOM() LIMIT 2
    for(int i=0;i<ui->tableWidget->rowCount();i++)
    {
        qDebug()<<"subjectid"<<ui->tableWidget->item(i,0)->data(Qt::UserRole).toInt();
        qDebug()<<"teststyleid"<<ui->tableWidget->item(i,1)->data(Qt::UserRole).toInt();
        qDebug()<<"number"<<ui->tableWidget->item(i,2)->text().toInt();
        QSqlQuery randomquery;
        randomquery.prepare(QString("SELECT id FROM test where subjectID=%1 and teststyleID=%2 ORDER BY RANDOM() LIMIT %3")
                         .arg(ui->tableWidget->item(i,0)->data(Qt::UserRole).toInt())
                         .arg(ui->tableWidget->item(i,1)->data(Qt::UserRole).toInt())
                         .arg(ui->tableWidget->item(i,2)->text().toInt()));
        qDebug()<<QString("SELECT id FROM test where subjectID=%1 and teststyleID=%2 ORDER BY RANDOM() LIMIT %3")
                  .arg(ui->tableWidget->item(i,0)->data(Qt::UserRole).toInt())
                  .arg(ui->tableWidget->item(i,1)->data(Qt::UserRole).toInt())
                  .arg(ui->tableWidget->item(i,2)->text().toInt());
        randomquery.exec();
        randomquery.last();
        int randomsize=randomquery.at()+1;
        //if ()
        //qDebug()<<"randomquery.size()"<<randomquery.size();
        randomquery.first();
        for (int j=0;j<randomsize;j++)
        {
            QSqlQuery randomquery_insert;
            randomquery_insert.prepare(QString("insert into testpaper_item(testid,testpaperid) values(%1,%2)")
                                       .arg(randomquery.value(0).toInt())
                                       .arg(m_wizard_PaperID));
            randomquery_insert.exec();
            qDebug()<<randomquery.value(0).toInt();
            randomquery.next();
        }

    }
    QWizard::accept();

}

